﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Internet_Cafe.App_Code.DatabaseModel;
using System.Data.Objects;

namespace Internet_Cafe.App_Code.DataBaseAccess
{
    public abstract class DbUsersAccess : DbAccess 
    {
        /// <summary>
        /// Listanje na site Users dodatno gi prikazuva koi se administratori a koi se workers
        /// </summary>
        /// <returns></returns>
        public static List<User> GetUsers()
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                List<User> userList = new List<User>();
                var obj = (from p in db.Users select p);
                foreach (User u in obj)
                {
                    userList.Add(u);
                }
                return userList;
            }
        }



        /// <summary>
        /// Brisi gi site korisnici i site nasleduvanja od niv (da ne recam cela baza :P )
        /// </summary>
        /// <returns></returns>
        public static bool DeleteAll()
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    var obj = (from p in db.Users select p);
                    db.DeleteObject(obj);
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                }
                return false;
            }
        }

        /// <summary>
        /// Vnesuvanje na korisnik (administrator ili vraboten)
        /// </summary>
        /// <param name="user"></param>
        /// <param name="setAsAdmin"></param>
        /// <returns></returns>
        public static string Insert(User user, bool setAsAdmin)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    string newID = Guid.NewGuid().ToString();
                    user.ID = newID;
                    db.AddToUsers(user);

                    if (setAsAdmin)
                    {
                        Administrator admin = new Administrator();
                        admin.ID = newID;
                        db.AddToAdministrators(admin);
                    }
                    else
                    {
                        Worker worker = new Worker();
                        worker.ID = newID;
                        db.AddToWorkers(worker);
                    }
                    db.SaveChanges();

                    return user.ID;
                }
                catch
                {
                }
                return null;
            }
        }


        /// <summary>
        /// Update na korisnik
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool Update(string userID, User user)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    User oldUser = (User)((from p in db.Users where p.ID == userID select p).First());
                    oldUser.FirstName = user.FirstName;
                    oldUser.LastName = user.LastName;
                    oldUser.Username = user.Username;
                    oldUser.Password = user.Password;
                    oldUser.Email = user.Email;
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                }
                return false;
            }
        }


        /// <summary>
        /// Brisenje na korisnik
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static bool Delete(string userID)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    User user = (User)((from p in db.Users where p.ID == userID select p).First());
                    db.DeleteObject(user);
                    db.SaveChanges();
                }
                catch
                {
                    return false;
                }
                return true;
            }
        }



        /// <summary>
        /// dali e OK username i password
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool IsValid(string username, string password)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    User usr = (from u in db.Users
                                where u.Username == username && u.Password == password
                                select u).First();
                    if (usr != null)
                        return true;
                }
                catch
                {
                }
                return false;
            }
        }


        /// <summary>
        /// Prevzemi administrator ako e validen
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static User GetAdmin(string username, string password)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    User user = (from a in db.Administrators
                                 join u in db.Users
                                 on a.ID equals u.ID
                                 where u.Username == username && u.Password == password
                                 select u).First();
                    return user;
                }
                catch
                {
                }
                return null;
            }
        }


        /// <summary>
        /// Prevzemi go Worker ako e validen
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static User GetWorker(string username, string password)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    User user = (from w in db.Workers
                                 join u in db.Users
                                 on w.ID equals u.ID
                                 where u.Username == username && u.Password == password
                                 select u).First();
                    return user;
                }
                catch
                {
                }
                return null;
            }
        }
    }
}
